package cn.changeforyou.web.cloud.blog.controller;

import cn.changeforyou.web.cloud.webBase.common.model.PageQueryModel;
import cn.changeforyou.web.cloud.webBase.common.model.Result;
import cn.changeforyou.web.cloud.webBase.controller.AbstractController;
import cn.changeforyou.web.cloud.blog.entity.dto.ArticleDTO;
import cn.changeforyou.web.cloud.blog.entity.po.Article;
import cn.changeforyou.web.cloud.blog.model.ArticleModel;
import cn.changeforyou.web.cloud.blog.service.ArticleService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;


/**
 * 博客前台处理器
 *
 * @author zhyu
 */
@RestController
@RequestMapping("/article")
public class ArticleFrontController extends AbstractController {

    @Resource
    private ArticleService articleService;
    private ObjectMapper objectMapper = new ObjectMapper();

    /**
     * 分页查询
     */
	@GetMapping("/getList")
	public Result<Page<Article>> getArticlePage(PageQueryModel<ArticleModel> model) {
		return Result.success(articleService.getArticlePage(model));
	}

	/**
	 * 获取单篇文章
	 */
	@GetMapping("{id}")
	public Result<Article> getById(@PathVariable("id") Integer id) {
		validIsNull(id);
		Article article = articleService.getById(id);
		return Result.success(article);
	}

	@PostMapping("saveOrUpdate")
	public Result<Article> saveOrUpdate(@Valid @RequestBody ArticleDTO article) {
		return Result.success(articleService.addOrUpdate(article));
	}

    /**
     * 获取全部标签
     */
    @GetMapping("/getTags")
    public Result<List<String>> searchTag() {
        return Result.success(articleService.getTags());
    }

}
